import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:yt_devops/components/event_bus_util/eventbus_util.dart';
import 'package:yt_devops/components/tips.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/services/user/user_services.dart';
import 'package:yt_devops/utils/func_utils.dart';

class LanguagePageController extends GetxController {
  UserServices services = Get.put(UserServices());
  int checkIndex = 0;
  List<String> languageList = ['中文(简体)', 'English'];
  @override
  void onInit() {
    print('Get.locale==' + Get.locale!.languageCode.toString());
    print('Get.locale==' + Get.locale!.countryCode.toString());

    super.onInit();
  }

  @override
  void onReady() {}

  @override
  void onClose() {}

  ///切换语言
  changeLanguage(int index) async {
    if(index == checkIndex){//如果选择索引和当前索引一致不做任何操作
      return;
    }
    var locale = const Locale('zh', 'CN');
    if (index == 1) {
      locale = const Locale('en', 'US');
    }
    await changeLanguageRequest(locale);
    FuncUtils.changeLocale(locale: locale);
    EventBusUtil.getInstance().fire(SwitchLanguage());
    GetStorage().write('changeLocale', locale.languageCode.toString());
    ListTips.resetRefresh( );
    Get.back();
  }

  Future changeLanguageRequest(Locale locale) async =>
      await Configs.httpClient.requestWithLoading(() async {
        await services
            .changeLanguage('${locale.languageCode}-${locale.countryCode}');
      });
}
